﻿#include "coredatabase.h"

#include <authority.h>
#include <menu.h>
#include <role.h>
#include <user.h>

QMutex CoreDatabase::mutex;
CoreDatabase* CoreDatabase::instance = nullptr;

CoreDatabase *CoreDatabase::getInstance()
{
    if (instance == nullptr) {
        QMutexLocker locker(&mutex);
        if (instance == nullptr) {
            instance = new CoreDatabase();
        }
    }
    return instance;
}

void CoreDatabase::deleteInstance()
{
    if (instance != nullptr) {
        delete instance;
        instance = nullptr;
    }
}

QSqlDatabase CoreDatabase::getConnection()
{
    return databasePool->getConnection();
}

void CoreDatabase::releaseConnection(QSqlDatabase &connection)
{
    databasePool->releaseConnection(connection);
}

CoreDatabase::CoreDatabase(QObject *parent) : QObject(parent)
{
    QString databasePoolConfigFileName = QCoreApplication::applicationDirPath() + "/config/coredatabase.ini";
    databasePoolSettings = new QSettings(databasePoolConfigFileName,QSettings::IniFormat, this);
    databasePoolSettings->beginGroup("database");
    databasePoolSettings->setValue("poolMark","core");
    databasePool = new QDatabasePool(databasePoolSettings);
}

CoreDatabase::~CoreDatabase()
{
    delete databasePool;
    delete databasePoolSettings;
}


